package copyengine.ui.infoTree.buildTree
{
    import avmplus.getQualifiedClassName;

    import copyengine.infoTree.meta.CESDisplayObjectMeta;
    import copyengine.log.CELog;
    import copyengine.resource.resProxy.CEResProxy;
    import copyengine.ui.infoTree.buildTree.builder.InfoTreeBuilderBasic;
    import copyengine.ui.infoTree.buildTree.builder.InfoTreeBuilderImage;
    import copyengine.ui.infoTree.buildTree.builder.InfoTreeBuilderMovieClip;
    import copyengine.ui.infoTree.buildTree.builder.InfoTreeBuilderPH;
    import copyengine.ui.infoTree.buildTree.builder.InfoTreeBuilderS3;
    import copyengine.ui.infoTree.buildTree.builder.InfoTreeBuilderS9;
    import copyengine.ui.infoTree.buildTree.builder.InfoTreeBuilderSprite;

    import flash.utils.Dictionary;

    public class InfoTreeBuilderBuildTreeHelper
    {
        private var mAllBuilderVector:Vector.<InfoTreeBuilderBasic>;
        private var mCurrentResProxy:CEResProxy;

        public function InfoTreeBuilderBuildTreeHelper()
        {
        }

        //================================//
        //==Initialize&Dispose  Function
        //================================//

        public function initialize():void
        {
            mAllBuilderVector = new Vector.<InfoTreeBuilderBasic>();
            doPushBuilderToVector(new InfoTreeBuilderImage());
            doPushBuilderToVector(new InfoTreeBuilderSprite());
            doPushBuilderToVector(new InfoTreeBuilderMovieClip());
            doPushBuilderToVector(new InfoTreeBuilderPH());
            doPushBuilderToVector(new InfoTreeBuilderS3());
            doPushBuilderToVector(new InfoTreeBuilderS9());
        }

        private function doPushBuilderToVector(_builder:InfoTreeBuilderBasic):void
        {
            _builder.setBuildHelper(this);
            mAllBuilderVector.push(_builder);
        }

        //==============================//
        //==Public Interface  Function
        //==============================//

        public function buildDisplayObject(_mcMeta:CESDisplayObjectMeta, _resProxy:CEResProxy):InfoTreeBuildResult
        {
            mCurrentResProxy = _resProxy;
            var buildResult:InfoTreeBuildResult = new InfoTreeBuildResult();
            buildResult.mcQuickFindDic = new Dictionary(true);
            buildResult.rootMc = getBuilderByMeta(_mcMeta).getBuildResultMc(_mcMeta, buildResult.mcQuickFindDic, "root", true);
            mCurrentResProxy = null;
            return buildResult;
        }

        public function getBuilderByMeta(_meta:*):InfoTreeBuilderBasic
        {
            for each(var builder:InfoTreeBuilderBasic in mAllBuilderVector) {
                if (builder.isCanHandleMeta(_meta)) {
                    builder.setResProxy(mCurrentResProxy);
                    return builder;
                }
            }
            CELog.err("InfoTreeBuilderBuildTreeHelper -> getBuilderByMeta can't any builder for meta :  " + getQualifiedClassName(_meta));
            return null;
        }
    }
}
